package com.juyu.reader.author.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.juyu.reader.author.model.Author;
import com.juyu.reader.author.model.Book;
import com.juyu.reader.author.model.BookChapter;
import com.juyu.reader.author.service.BookChapterService;
import com.juyu.reader.author.service.BookService;
import com.juyu.reader.author.util.StringUtil;

public class VerifyInterceptor implements HandlerInterceptor {

	@Autowired
	private BookService bookService;
	@Autowired
	private BookChapterService bookChapterService;

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		// TODO Auto-generated method stub
		Author author = (Author) request.getSession().getAttribute(Configure.systemSessionAuthor);
		if (author != null) {
			int bookId = 0;
			int chapterId = 0;
			try {
				String bookIdStr = request.getParameter("bookId");
				String chapterIdStr = request.getParameter("chapterId");
				if (!StringUtil.isNullOrEmpty(bookIdStr)) {
					bookId = Integer.valueOf(bookIdStr);
				}
				if (!StringUtil.isNullOrEmpty(chapterIdStr)) {
					chapterId = Integer.valueOf(chapterIdStr);
				}
			} catch (Exception e) {
			}
			if ((bookId <= 0 && chapterId <= 0) || bookId < 0 || chapterId < 0) {
				request.getRequestDispatcher("/author/error").forward(request, response);
				return false;
			}
			// 验证书籍是否属于该作者
			if (bookId > 0) {
				Book findBook = new Book();
				findBook.setAuthorId(author.getAuthorId());
				findBook.setBookId(bookId);
				findBook.setBookFlag(-1);
				List<Book> bookList = bookService.findBook(findBook);
				if (bookList.size() != 1) {
					request.getRequestDispatcher("/author/error").forward(request, response);
					return false;
				}
			}
			// 验证章节是否属于该作者
			if (chapterId > 0) {
				List<BookChapter> bookChapterList = bookChapterService.findBookChapterById(chapterId);
				if (bookChapterList.size() != 1) {
					request.getRequestDispatcher("/author/error").forward(request, response);
					return false;
				} else {
					Book findBook = new Book();
					findBook.setAuthorId(author.getAuthorId());
					findBook.setBookId(bookChapterList.get(0).getBookId());
					findBook.setBookFlag(-1);
					List<Book> bookList = bookService.findBook(findBook);
					if (bookList.size() != 1) {
						request.getRequestDispatcher("/author/error").forward(request, response);
						return false;
					}
				}
			}
			return true;
		} else {
			request.getRequestDispatcher("/author/toLogin").forward(request, response);
			return false;
		}
	}

}
